﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using IntelligentDecisioning.SharePoint.Definition;
using IntelligentDecisioning.SharePoint.Helpers;
using IntelligentDecisioning.SharePoint.Logging;
using IntelligentDecisioning.SharePoint.Definition.Fields;

namespace IntelligentDecisioning.SharePoint.SiteProvisioningProviders
{
    /// <summary>
    /// A base class fro provisioning sites
    /// </summary>
    public abstract class IDBaseSiteProvisioningProvider: SPWebProvisioningProvider
    {
        #region Data

        protected SPSite SiteCollection { get; set; }
        protected SPWeb RootWeb { get; set; }
        protected SPWeb CurrentWeb { get; set; }
        public SPWebProvisioningProperties Properties { get; set; }
     

        #endregion

        #region Helpers 

        protected FeatureInstallationLog _installLog;

        #endregion

        #region Provision the Site !!

        public override sealed void Provision(SPWebProvisioningProperties properties)
        {
            InitialiseSiteProvisioningProvider(properties);

            //Apply web templates - causes the CAML site provisioning files to be applied
            //Add the id's of any required features to ONET.xml
            CurrentWeb.ApplyWebTemplate(GetSiteTemplateName());
        }

        #endregion

        #region Base Site Framework Implementation

        #region Initialise

        private void InitialiseSiteProvisioningProvider(SPWebProvisioningProperties properties)
        {
            if (properties == null) throw new ArgumentNullException("SPWebProvisioningProperties");

            Properties = properties;
            CurrentWeb = properties.Web;
            SiteCollection = Properties.Web.Site;
            
            _installLog = new FeatureInstallationLog(properties.Web.Name, Guid.Empty, properties.Web.Site);
        }

        #endregion

        #region GetSiteTemplateName (Abstract)

        protected abstract String GetSiteTemplateName();

        #endregion

        #endregion
    }
}
